﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Reflection;
using System.Net;

namespace Yugi.Client.ViewModel
{
    public partial class MainServerHandler
    {
        internal User.ClientManager client = new User.ClientManager();
        public Window win;
        public bool OfflineTest;

        public LoginModel Login_Model { get; set; }
        public RoomModel Room_Model { get; set; }
        public DeckConstructModel Deck_Model { get; set; }
        public ChatManagerModel ChatManager_Model { get; set; }
        public ListOnlineModel ListOnline_Model { get; set; }
        public ExternalModel External_Model { get; set; }
        public YugiohVietNamModel YugiohVietNam_Model { get; set; }

        public int Current_Table_ID { get; set; }
        public int Current_Room_ID { get; set; }
        public MainServerHandler(Window win,bool offline_test)
        {
            Current_Room_ID = -1;
            Current_Table_ID = -1;
            this.win = win;
            this.OfflineTest = offline_test;
            client.login_server_connection.Receive_From_Login_Server += new EventHandler<User.YEventArgs.ReceiveEventArgs>(login_server_connection_Receive_From_Login_Server);
            Init();
        }
        private void Init()
        {
            Login_Model = new LoginModel();
            Room_Model = new RoomModel();
            Deck_Model = new DeckConstructModel();
            ChatManager_Model = new ChatManagerModel(this);
            ListOnline_Model = new ListOnlineModel();
            External_Model = new ExternalModel();
            YugiohVietNam_Model = new YugiohVietNamModel();
        }
        public void Connect()
        {
            IPAddress ip = IPAddress.Parse(Program.IP_LOGIN_SERVER);
            int port = Program.PORT_LOGIN_SERVER;
            client.login_server_connection.Connect(ip,port);

        }
        void login_server_connection_Receive_From_Login_Server(object sender, User.YEventArgs.ReceiveEventArgs e)
        {
            Common.MSG_GSM mss = (Common.MSG_GSM)e.data[0];
            byte[] d = new byte[e.data.Length - 1];
            Buffer.BlockCopy(e.data, 1, d, 0, e.data.Length - 1);
            Action invoke = () =>
            {
                this.ReciveMessageFromServer(mss, d);
            };
            this.win.Dispatcher.Invoke(invoke);
        }
        public void ReciveMessageFromServer(Common.MSG_GSM message, byte[] param)
        {
            Type t = typeof(MainServerHandler);
            MethodInfo handler = t.GetMethod("ConvertReciveMainServer_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
            List<object> res_conver=null;
            try
            {
                res_conver = (List<object>)handler.Invoke(this, new object[] { param });
            }
            catch 
            {
                System.Windows.MessageBox.Show("Error conver: "+message);
            }
            handler = t.GetMethod("ProcessMainServer_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
            try
            {
                if(res_conver!=null)
                    handler.Invoke(this, new object[] { res_conver.ToArray() });
                else
                    System.Windows.MessageBox.Show("error: res_conver = null");
            }
            catch
            {
                System.Windows.MessageBox.Show("Error Process : " + message);
            }
        }
        public void SendMessageToServer(Common.MSG_GSM message, params object[] param)
        {

            try
            {
                Type t = typeof(MainServerHandler);
                MethodInfo handler = t.GetMethod("ConvertSendMainServer_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                if (handler == null)
                    MessageBox.Show("handler = null");
                object res_convert= handler.Invoke(this, new object[] { param });
                List<byte> tmp = ((byte[])res_convert).ToList();
                tmp.Insert(0, (byte)message);
                if (!OfflineTest)
                    //socket.SendMessage(converter.Result_Send.ToArray());
                    client.login_server_connection.SendMessage(tmp.ToArray());
            }
            catch (System.Exception ex)
            {
                System.Windows.MessageBox.Show("Error Process Send MainServer : " + message);
            }
        }
      
    }
}
